package com.android.car_bluetooth_audio.ble.scanner;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;



import java.util.List;

public class PermissionHelper {



    private static String[] getBle() {
        String[] permissions;
        if (Build.VERSION.SDK_INT < 23) {
            permissions = null;
        } else {
            int var1 = Build.VERSION.SDK_INT;

            if (31 > var1) {
                permissions = new String[]{"android.permission.ACCESS_COARSE_LOCATION", "android.permission.ACCESS_FINE_LOCATION"};
                return permissions;
            }

            permissions = new String[]{"android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT"};
        }

        return permissions;
    }

    public static boolean hasPermissions(Context context){
        return hasPermissions(context, getBle());
    }

    public static boolean hasPermissions(Context context, String[] permissions) {

        for (String permission:permissions){


            if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
//        Collection var3 = (Collection)permissions;
//        if (var3 == null || var3.isEmpty()) {
//            return true;
//        } else {
//            Iterator var4 = permissions.iterator();
//
//            String permission;
//            do {
//                if (!var4.hasNext()) {
//                    return true;
//                }
//
//                permission = (String)var4.next();
//            } while(ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED);
//
//            return false;
//        }
    }

    public static void requestBle(Activity context,CallBack callBack) {

//        requestPermission(context,getBle(),callBack);
    }


//    private static void requestPermission(Activity activity, String[] permissions,CallBack callBack) {
//
//        if (permissions != null) {
//            if (permissions.length!=0){
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                    PermissionX.init((FragmentActivity) activity).permissions(permissions)
//                            .onExplainRequestReason(new ExplainReasonCallback() {
//                        @Override
//                        public void onExplainReason(@NonNull ExplainScope scope, @NonNull List<String> deniedList) {
//                            scope.showRequestReasonDialog(
//                                    deniedList,
//                                    activity.getString(R.string.permission_explain_msg),
//                                    activity.getString(android.R.string.ok),
//                                    activity.getString(android.R.string.cancel)
//                                    );
//                        }
//                    })
//                            .onForwardToSettings(new ForwardToSettingsCallback() {
//                                @Override
//                                public void onForwardToSettings(@NonNull ForwardScope scope, @NonNull List<String> deniedList) {
//                                    scope.showForwardToSettingsDialog(
//                                            deniedList,
//                                            activity.getString(R.string.permission_to_settings_msg),
//                                            activity.getString(android.R.string.ok),
//                                            activity.getString(android.R.string.cancel)
//                                            );
//                                }
//                            })
//                            .explainReasonBeforeRequest()
//                            .request(new RequestCallback() {
//                                @Override
//                                public void onResult(boolean allGranted, @NonNull List<String> grantedList, @NonNull List<String> deniedList) {
//                                    Log.e("及人员这里了吗","硕大无朋"+allGranted);
//                                    if (callBack!=null){
//                                        callBack.isPermissionGranted(allGranted);
//                                    }
//
//                                }
//                            });
////                    ((Activity)context).requestPermissions(permissions, 101);
//                }
//            }
//        }
//    }

    public interface CallBack{
        void isPermissionGranted(boolean allGranted);
    }

}
